<?php
/*
 * @Author: an
 * @Date: 2021/12/17
 * @Description: 频道管理
 */


namespace App\Services\Aliyun\AudioAndVideo;


use App\Services\Aliyun\BaseService;
use App\Services\Configs\DefaultService;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Rtc\V20180111\Rtc;
use AlibabaCloud\SDK\Rtc\V20180111\Models\DescribeChannelParticipantsRequest;
use AlibabaCloud\SDK\Rtc\V20180111\Models\DescribeUserInfoInChannelRequest;
use AlibabaCloud\SDK\Rtc\V20180111\Models\DescribeChannelUsersRequest;
use AlibabaCloud\SDK\Rtc\V20180111\Models\RemoveTerminalsRequest;
use AlibabaCloud\SDK\Rtc\V20180111\Models\DeleteChannelRequest;

class ChannelService extends BaseService
{
    private static $accessKeyId = '';
    private static $accessKeySecret = '';

    public function __construct()
    {
        $config = $this->getConfig(DefaultService::ALI_AV);
        self::$accessKeyId = $config['accessKeyId'];
        self::$accessKeySecret = $config['accessKeySecret'];
    }

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Rtc Client
     */
    public static function createClient($accessKeyId, $accessKeySecret)
    {
        $config = new Config([
            "accessKeyId" => $accessKeyId,
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "rtc.aliyuncs.com";
        return new Rtc($config);
    }

    /**
     * 查询频道在线用户列表，不包含详细信息
     * @param $appId 必填 应用ID，可通过控制台创建和查询，仅支持传单个ID。
     * @param $channelId 必填 加入的频道，仅支持传单个ID。
     * @param string $order 非必填 排序方式，取值： asc：递增。desc（默认值）：递减。
     * @param int $pageNum 非必填 第几页，默认查询第1页。
     * @param int $pageSize 非必填 每页显示用户数量，默认每页显示10条用户数据。
     * @return array
     * @author an
     * @time 2021-12-17 17:19
     */
    public function describeChannelParticipants($appId, $channelId, $order = 'asc', $pageNum = 1, $pageSize = 10)
    {
        try {
            $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
            $describeChannelParticipantsRequest = new DescribeChannelParticipantsRequest([
                "appId" => $appId,
                "channelId" => $channelId,
                "order" => $order,
                "pageNum" => $pageNum,
                "pageSize" => $pageSize
            ]);
            // 复制代码运行请自行打印 API 的返回值
            $data = $client->describeChannelParticipants($describeChannelParticipantsRequest);
            return ['code' => 0, 'msg' => 'ok', 'data' => $data->body];
        } catch (\Exception $exception) {
            return ['code' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 查询用户在频道中实时信息
     * @param $appId 必填 应用ID，通过控制台创建和查询，仅支持传单个ID。
     * @param $channelId 必填 频道ID，仅支持传单个ID。
     * @param $userId 必填 用户ID，仅支持传单个ID。
     * @return array
     * @author an
     * @time 2021-12-17 17:34
     */
    public function describeUserInfoInChannel($appId, $channelId, $userId)
    {
        try {
            $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
            $describeUserInfoInChannelRequest = new DescribeUserInfoInChannelRequest([
                "userId" => $userId,
                "channelId" => $channelId,
                "appId" => $appId
            ]);
            // 复制代码运行请自行打印 API 的返回值
            $data = $client->describeUserInfoInChannel($describeUserInfoInChannelRequest);
            return ['code' => 0, 'msg' => 'ok', 'data' => $data->body];
        } catch (\Exception $exception) {
            return ['code' => 1, 'msg' => $exception->getMessage()];
        }

    }


    /**
     * 查询 Channel（频道）实时在线用户列表
     * @param $appId 必填 应用ID，通过控制台创建和查询，仅支持传单个ID。
     * @param $channelId 必填 频道ID，仅支持传单个ID。
     * @return array
     * @author an
     * @time 2021-12-17 17:39
     */
    public function describeChannelUsers($appId, $channelId)
    {
        try {
            $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
            $describeChannelUsersRequest = new DescribeChannelUsersRequest([
                "appId" => $appId,
                "channelId" => $channelId
            ]);
            // 复制代码运行请自行打印 API 的返回值
            $data = $client->describeChannelUsers($describeChannelUsersRequest);
            return ['code' => 0, 'msg' => 'ok', 'data' => $data->body];
        } catch (\Exception $exception) {
            return ['code' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 将指定终端用户从频道踢出
     * @param $appId 必填 应用ID，通过控制台创建和查询，仅支持传单个ID。
     * @param $channelId 必填 已加入的频道ID，仅支持传单个ID。
     * @param $terminalIds 必填 用户ID列表。数组
     * @return array
     * @author an
     * @time 2021-12-17 17:43
     */
    public function removeTerminals($appId, $channelId, $terminalIds)
    {
        try {
            if (!is_array($terminalIds)) {
                throw new \Exception('terminalIds必须是数组');
            }
            $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
            $removeTerminalsRequest = new RemoveTerminalsRequest([
                "terminalIds" => $terminalIds,
                "appId" => $appId,
                "channelId" => $channelId
            ]);
            // 复制代码运行请自行打印 API 的返回值
            $data = $client->removeTerminals($removeTerminalsRequest);
            return ['code' => 0, 'msg' => 'ok', 'data' => $data->body];
        } catch (\Exception $exception) {
            return ['code' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 删除频道
     * @param $appId 必填 应用ID，通过控制台创建和查询，仅支持传单个ID。
     * @param $channelId 必填 已加入的频道ID，仅支持传单个ID。
     * @return array
     * @author an
     * @time 2021-12-17 17:49
     */
    public function deleteChannel($appId, $channelId)
    {
        try {
            $client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
            $deleteChannelRequest = new DeleteChannelRequest([
                "appId" => $appId,
                "channelId" => $channelId
            ]);
            // 复制代码运行请自行打印 API 的返回值
            $data = $client->deleteChannel($deleteChannelRequest);
            return ['code' => 0, 'msg' => 'ok', 'data' => $data->body];
        } catch (\Exception $exception) {
            return ['code' => 1, 'msg' => $exception->getMessage()];
        }
    }
}
